package com.bong.panpan.bongcharttools.custom;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.util.ArrayMap;
import android.view.inputmethod.InputMethodManager;

import com.bong.panpan.bongcharttools.util.Common;

/**
 * Created by panpan on 2017/10/24.
 */

public class MyUtil {
    public static int byte2Int(byte data) {
        if (data < 0)
            return data + 256;
        return data;
    }

    public static int[] byte2IntArray(byte... data) {
        int[] result = new int[data.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = byte2Int(data[i]);
        }
        return result;
    }

    public static int uint16_decode(byte... p_encoded_data) {
        return byte2Int(p_encoded_data[0]) | (byte2Int(p_encoded_data[1]) << 8);
    }

    public static int uint2Int(int num) {
        if ((num & 0xFFFF0000) == 0) {
            if (num < 0x8000)
                return num;
            else
                return num - 0x10000;
        } else if ((num & 0xFF000000) == 0) {
            if (num < 0x800000)
                return num;
            else
                return num - 0x1000000;
        }
        return 0;
    }

    public static ArrayMap<Integer, int[]> analyzeAdvData(byte[] adv) {
        ArrayMap<Integer, int[]> result = new ArrayMap();
        int data[] = byte2IntArray(adv);
        int type, length;
        for (int i = 0; i < data.length; i++) {
            length = data[i];
            i++;
            type = data[i];
            int[] value = new int[length - 1];
            for (int j = 0; j < length - 1; j++) {
                i++;
                value[j] = data[i];
            }
            result.put(type, value);
        }
        return result;
    }

    public static boolean isContainHeartRateServer(byte[] adv) {
        int data[] = byte2IntArray(adv);
        int type, length;
        boolean ok1 = false, ok2 = false;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < data.length; ) {
            length = data[i++];
            if (length == 0)
                return false;
            type = data[i++];
            stringBuilder.delete(0, stringBuilder.length());
            for (int j = 0; j < length - 1; j++) {
                stringBuilder.append(String.format("%02X", data[i++]));
            }
            if (type == 0x02 || type == 0x03) {
                if (stringBuilder.indexOf("0D18") != -1) {
                    ok1 = true;
                }
            } else if (type == 0x19) {
                if (stringBuilder.indexOf("4103") != -1) {
                    ok2 = true;
                }
            } else if (type == 0x09) {
                if (stringBuilder.indexOf("506F6C6172") != -1)//判断名字 Polar
                {
                    return true;
                }
            }
            if (ok1 || ok2)
                return true;
        }
        return false;
    }

    public static void showToast(Context context, String text) {
        Common.showToast(context, text);
    }

    public static void showAlertDialog(Context context, String title, String msg, AlertDialog.OnClickListener listener) {
        AlertDialog alertDialog = null;
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title);
            builder.setPositiveButton("确定", listener);
            builder.setNegativeButton("取消", null);
            alertDialog = builder.create();
        }
        alertDialog.setMessage(msg);
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.BLUE);
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED);
    }

    /**
     * 关闭软键盘
     */
    public static void closeKeybord(Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 得到InputMethodManager的实例
        if (imm.isActive()) {
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                    InputMethodManager.HIDE_NOT_ALWAYS);
            //imm.hideSoftInputFromInputMethod(mContext.getWindow().peekDecorView().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * @param bytes 文件的大小（字节）
     * @return 文件大小描述的字符串（eg：input:1024 output:1Kb）
     */
    public static String getFileLengthString(int bytes) {
        if (bytes > 1024) {
            return String.format("%.2fKb", bytes / 1024.0f);
        } else if (bytes > 1024 * 1024) {
            return String.format("%.2fMb", bytes / 1024.0f / 1024);
        } else
            return String.format("%dBytes", bytes);
    }
}
